//
//  HomeSectionFour.m
//  JiaoX
//
//  Created by yinqy on 15/1/29.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeSectionFour.h"
#import "HomeProdList.h"
#import "ProductItemInfo.h"
#import "LeftLineView.h"
#import "ProdCateHViewCell.h"
#import "HomeFirstPageHeaderView.h"

#define TITLE_FONT_SIZE [DimensUtils getScaleX:14]
@interface HomeSectionFour()<UICollectionViewDataSource,UICollectionViewDelegate>{
    UICollectionView *_collectionView;
    HomeProdList *_prodList;
}
@end
@implementation HomeSectionFour
- (instancetype)initWithHomeProdList:(HomeProdList *)prodList{
    self = [super init];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        
        _prodList = prodList;
        LeftLineView *title = [[LeftLineView alloc]initWithTitleFontSize:TITLE_FONT_SIZE];
        [title setText:prodList.title];
        title.label.textColor = JXColor(51, 51, 51);
        title.x = JXPublicInset;
        title.y = JXPublicInset;
        [self addSubview:title];
        
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
        UICollectionView *collectView  =[[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), JXScreenW, prodList.list.count/2 * (PRODCATEHVIEW_SMALL_HEIGHT +JXPublicInset)+JXPublicInset) collectionViewLayout:flowLayout];
        collectView.backgroundColor = JXGlobalColorBg;
        [collectView registerClass:[ProdCateHViewCell class] forCellWithReuseIdentifier:PROD_IDENTIFIER];
        collectView.delegate = self;
        collectView.dataSource=self;
        _collectionView = collectView;
        [self addSubview:collectView];
        [self setSize:CGSizeMake(JXScreenW, CGRectGetMaxY(collectView.frame)+JXPublicInset)];
    }
    return self;
}


#pragma Mark - UICollectionView DataSource & delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _prodList.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ProdCateHViewCell  *cell = [ProdCateHViewCell cellWithTableView:collectionView indexPath:indexPath];
    [cell setProdData:_prodList.list[indexPath.row] isAdd:false];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((JXScreenW-JXPublicInset*3)/2, PRODCATEHVIEW_SMALL_HEIGHT);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ProductItemInfo *itemInfo = _prodList.list[indexPath.row];
    
    if ([self.superview isKindOfClass:[HomeFirstPageHeaderView class]]) {
        HomeFirstPageHeaderView *view  =(HomeFirstPageHeaderView *) self.superview;
        if ([view.delegate respondsToSelector:@selector(sectionFourClick:)]) {
            [view.delegate sectionFourClick:itemInfo];
        }
    }
}




@end
